<script setup>
import { ref,reactive,onMounted } from 'vue';
import { Grid, GridItem, Image, Tag, Icon } from 'vant';
import axios from "@/utils/axios.js"
import {useUserStore} from "@/stores/user.js";
import {useRouter} from "vue-router";

const router = useRouter();
const users = useUserStore().user
// 模拟数据
const items = ref([
]);
const us = JSON.parse(sessionStorage.getItem("user"));
const init=()=>{
  axios.get(`api/entryCollects/getEntryById/${us.userId}`)
      .then(res=>{
        if(res.code === 200){
          items.value = res.data
        }
        else{
          ElMessage({
            message: "请求失败",
            type: "error",
            duration: 1500,
          })
        }
      })
}
onMounted(() => {
  init();
})

// 格式化时间
const formatDate = (dateString) => {
  const date = new Date(dateString);
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
};

const goToEntry=(val)=>{
  router.push({ name: 'entryWithId', params: { entryId: val } });
}
</script>

<template>
  <van-nav-bar
      title="我的收藏"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
      safe-area-inset-top
  />
  <div style="height: 46px;"></div>

  <div class="header">
    <h1>我的收藏历史</h1>
    <p>查看您收藏过的所有词条</p>
  </div>

  <div class="favorites-page">
    <Grid :column-num="2" :gutter="10">
      <GridItem v-for="item in items" :key="item.id" @click="goToEntry(item.entryId)">
        <div class="favorite-item">
          <Image
              :src="item.entriesPic"
              fit="cover"
              height="120"
              radius="4"
              class="cover-image"
          />
          <div class="item-info" >
            <h3 class="item-title">{{ item.entryName }}</h3>
            <div class="item-meta">
              <Tag plain type="primary" size="small">
                <Icon name="clock-o" size="12" />
                {{ formatDate(item.createdAt) }}
              </Tag>
            </div>
            <div class="item-stats">
              <span class="stat">
                <Icon name="like-o" size="14" />
                {{ item.likes }}
              </span>
              <span class="stat">
                <Icon name="star-o" size="14" />
                {{ item.collects }}
              </span>
            </div>
          </div>
        </div>
      </GridItem>
    </Grid>
  </div>
</template>

<style scoped>
/* 修改后的header样式 */
.header {
  text-align: center;
  padding: 24px 0;
  background-color: #ff6b00; /* 橙色背景 */
  color: white; /* 白色字体 */
  margin-bottom: 20px;
}

.header h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 500;
}

.header p {
  margin: 8px 0 0;
  font-size: 14px;
  opacity: 0.9;
}

.favorites-page {
  padding: 12px;
  background-color: #f7f8fa;
}

.favorite-item {
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  margin-bottom: 10px;
}

.cover-image {
  width: 100%;
  display: block;
}

.item-info {
  padding: 10px;
}

.item-title {
  font-size: 14px;
  margin: 0 0 8px 0;
  color: #333;
  font-weight: normal;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.item-meta {
  margin-bottom: 8px;
}

.item-stats {
  display: flex;
  align-items: center;
  color: #969799;
  font-size: 12px;
}

.stat {
  display: flex;
  align-items: center;
  margin-right: 12px;
}

.stat .van-icon {
  margin-right: 4px;
}
</style>